
package guoan.service;

import guoan.domain.AdvPay;
import guoan.repository.AdvPayRepository;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = false)
public class AdvPayService {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private AdvPayRepository advPayRepository;

    public AdvPay findOne(String type){
        // TODO Auto-generated method stub
        AdvPay advPay = advPayRepository.findTop1ByType(type);
        if (advPay == null) {
            advPay = new AdvPay();
            advPay.setCreateTime(new DateTime());
            advPay.setIsKey(0L);
            advPay.setType(type);
            save(advPay);
        }
        return advPay;
    }

    public void save(AdvPay advPay){
        if(advPay.getId()!=null){
            AdvPay oldAdvPay = advPayRepository.findOne(advPay.getId());
            oldAdvPay.setInfo(advPay.getInfo());
            oldAdvPay.setCreateTime(new DateTime());
            oldAdvPay.setIsKey(advPay.getIsKey());
            oldAdvPay.setKeyName(advPay.getKeyName());
            advPayRepository.save(oldAdvPay);
        }else{
            advPayRepository.save(advPay);
        }

    }
}
